/* data consumer interface for mapping data to GUI automaticly */

jsx3.lang.Class.defineInterface(
	"ux.cdb.admin.dataautomate.IDataConsumer",
	null,
	function(plugin, plugin_prototype) {

		plugin_prototype.applyData = jsx3.lang.Method.newAbstract("data");

		plugin_prototype.registerConsumer = function(mode) {
			if(!this.provider) {
				this.provider = this.findAncestor(function(obj){return obj.instanceOf("ux.cdb.admin.dataautomate.IDataProvider");},false);
			}
			if(this.provider){
				this.provider.unregisterConsumer(this);
				this.provider.registerConsumer(this);
			}
		};

		plugin_prototype.unregisterConsumer = function() {
			if(!this.provider) {
				this.provider = this.findAncestor(function(obj){return obj.instanceOf("ux.cdb.admin.dataautomate.IDataProvider");},false);
			}
			if(this.provider){
				this.provider.unregisterConsumer(this);
			}
		};

		plugin_prototype.setDataAttributeName = function(val){
			this.uxdataatt = val;
		};
		
		plugin_prototype.getDataAttributeName = function(){
			return this.uxdataatt;
		};
		
		plugin_prototype.setData = function(data) {
			//data is cdf document
			var me = this;
			setTimeout(function() {
				var ret = data;
				var id = null;
				if(ret) {
					var child = ret.getFirstChild();
					
					ret = {};
					var iterAtts = child.getAttributes().iterator();
					while(iterAtts.hasNext()) {
						var att = iterAtts.next();
						ret[att.getNodeName()] = att.getValue();
					}
					ret.id = id = child.getAttribute("jsxid");
				}
				me.applyData(ret);
			},0);
		};

        plugin_prototype.setDataByObject = function(data) {
            //data is simple JSON object
            var me = this;
            setTimeout(function() {
                me.applyData(data);
            },0);
        };
        
        plugin_prototype.setSimpleData = function(data) {
            //data is primitive type value.
            var attr = this.getDataAttributeName();
            var ret = {};
            ret[attr] = data;
            this.setDataByObject(ret);
        };

		plugin_prototype.setFormatHandler = function(handler) {
			this.formatHandler = handler !== null ? handler + "" : null;
		};

		plugin_prototype.getFormatHandler = function() {
			return this.formatHandler;
		};

	});